
var EpyUpdate = new Class({
    Implements: Options,
    options: {
        'tagid':''
    },
    initialize: function(opts) {
        this.setOptions(opts);

        this.panel = document.id(this.options.tagid);
        if(!this.panel) return;
        this.panel.empty(); // clean panel

        //
        this.statRequest = new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new ShowBox().say('Open...');
                new Loader().show();
            },
            onComplete: function(resp) {
                var data = [];
                try {
                    data = eval('[' + resp + ']');
                }
                catch(err) { alert('Error...\n' + err); return; }
                new Loader().hide();

                this.createTable(data);
            }.bind(this)
        }).send('table=stat&nc=' + Date.now());

    },
    createTable: function(data) {
        this.content = new Element('div#content');
        this.listData = new Element('table#listData', {'rules':'groups'});
        this.tbody = new Element('tbody');

        this.panel.adopt(
            new Element('div.tl[html=<div class="tr"><div>&nbsp;</div></div>]'),
            this.content.adopt(this.listData),
            new Element('div.bl[html=<div class="br"><div>&nbsp;</div></div>]')
        )

        // make colgroups and head
        this.listData.adopt(
            new Element('thead').adopt(
                new Element('tr').adopt(
                    new Element('th.brdCol.center.w50[text=Name]'),
                    new Element('th.brdCol.center.w150[text=Make]'),
                    new Element('th.brdCol.center.w150[text=Dir]'),
                    new Element('th.brdCol.center.w50[text=Amazon]'),
                    new Element('th.brdCol.center.w50[text=AmzComp]'),
                    new Element('th.brdCol.center.w50[text=Ebay]'),
                    new Element('th.brdCol.center.w50[text=EbayA]'),
                    new Element('th.brdCol.center.w50[text=Buycom]'),
                    new Element('th.brdCol.center.w50[text=Time]'),
                    new Element('th.brdCol.center.w50[text=Build]')
                )
            ),
            new Element('tfoot').adopt(
                new Element('tr').adopt(
                    new Element('th.tdFull[html=&nbsp;]',{'colspan': 10})
                )
            ),
            this.tbody
        );

        this.tbody.empty();

        var advert;
        for(var i = 0; i < data.length; i++) {
            advert = data[i];

            this.tbody.adopt(
                new Element('tr').adopt(
                    new Element('td.brdCol.center.w50[text=' + advert.name + ']'),
                    new Element('td.brdCol.center.w150[html=<a href="../' + advert.url + '" target="_blank">Make ' + advert.name + '</a>]'),
                    new Element('td.brdCol.center.w150[html=<a href="' + advert.dir + '" target="_blank">Open ' + advert.name + '</a>]'),
                    new Element('td.brdCol.center.w50[text=' + advert.cntAmazon + ']'),
                    new Element('td.brdCol.center.w50[text=' + advert.cntAmzComp + ']'),
                    new Element('td.brdCol.center.w50[text=' + advert.cntEbay + ']'),
                    new Element('td.brdCol.center.w50[text=' + advert.cntEbayAli + ']'),
                    new Element('td.brdCol.center.w50[text=' + advert.cntBuycom + ']'),
                    new Element('td.brdCol.center.w50[text=' + advert.time + 'ms]'),
                    new Element('td.brdCol.center.w50[text=' + Date.parse(advert.bld).format('%a %b %d %Y') + ']')
                )
            );

        }
    }
});